home *** CD-ROM | disk | FTP | other *** search
- Path: cais.cais.com!usaid
- From: usaid@cais.cais.com (USAID)
- Newsgroups: comp.lang.c
- Subject: Question about sscanf
- Date: 8 Feb 1996 23:42:18 GMT
- Organization: Capital Area Internet Service info@cais.com 703-448-4470
- Message-ID: <4fe1oq$kuf@zippy.cais.net>
- NNTP-Posting-Host: cais.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Here is a fragment of code that I'm having a problem with. The program
- is bombing on the line containing the call to sscanf. It assigns to the
- first variable fine, but blows up on the second one. I have no idea
- what's wrong.
-
- int formatFile(void)
- {
- int i;
- char line[MAX_LINE_LEN + 1];
- char *name;
- char *value;
- char *val[MAXREGIONS];
- char *delimiter = ", ";
- FILE *ifp, *ofp;
-
- /* open input and output files */
- ifp = fopen("/opt/basis/tcs/formdata/test.txt", "r");
- ofp = fopen("/opt/basis/tcs/formdata/import.txt", "w");
- if ((ifp == NULL) || (ofp == NULL))
- return (0);
-
- while (fgetline(ifp, line, MAX_LINE_LEN) != -1)
- {
- /* store the name and value in variables */
- if (sscanf(line, "%s %s", name, value) != 2)
- {
- printf("Bad input from sscanf\n");
- return (0);
- }
- .
- .
- .
- }
-
- }
-
- The program doesn't even get to the error check; it bombs somewhere in
- sscanf. the function fgetline is a function I wrote; it parses the file,
- reads each line a character at a time. It stops at \n or EOF, and sticks
- the line into a null-terminated char array. It works like it's supposed
- to, in that it gets the first line of the file on the first iteration of
- the while, then bombs at the sscanf. The data is separated by spaces.
- sscanf assigns to name, but the only thing in value is garbage when I
- check it with the debugger. My code is similar to examples in a book I
- have. I'm working in UNIX (Solaris 2.4), in case that helps.
-
- Thanks in advance,
- Jed Prentice
-